CLASS ZCL_PLM_IN_001 DEFINITION
  PUBLIC

  CREATE PUBLIC .
  PUBLIC SECTION.
    DATA: cs_ztiflog TYPE ztiflog.

    INTERFACES if_http_service_extension .
  PROTECTED SECTION.


  PRIVATE SECTION.
  " ZPLMUSER
  " @U$(4/z=}FU7aAeH>oNBg7}X>uSkdjV2=tGx/q#(
ENDCLASS.



CLASS ZCL_PLM_IN_001 IMPLEMENTATION.


  METHOD if_http_service_extension~handle_request.
    DATA abap_context_info TYPE REF TO cl_abap_context_info.
    abap_context_info = NEW #( ).
    GET TIME STAMP FIELD DATA(lv_timestamp).

    DATA(lv_text) = request->get_text( )." 获取JSON

    DATA(lv_method)  =  request->get_method( ).

    TRY.
        cs_ztiflog-log_uuid = cl_system_uuid=>create_uuid_x16_static( ).
      CATCH cx_uuid_error.
        "handle exception
    ENDTRY.
    cs_ztiflog-comm_scenario = 'ZPLM_COM_001'.
    cs_ztiflog-comm_system_id = 'ZPLM_CS_001'.
    cs_ztiflog-function = 'ZPLM_IN_001_HTTP'.
    cs_ztiflog-zclass =  SY-repid." 'ZCL_API_BOM_SRV'.
    cs_ztiflog-sdate = abap_context_info->get_system_date( ).
    cs_ztiflog-stime = abap_context_info->get_system_time( ).
    cs_ztiflog-method = lv_method.
    cs_ztiflog-json_i = lv_text.
    cs_ztiflog-direction = 'INBOUND'.
    cs_ztiflog-json_length_i = strlen( lv_text ) .
    cs_ztiflog-timestamp_i = lv_timestamp.
    cs_ztiflog-usr = abap_context_info->get_user_technical_name( ).
    cs_ztiflog-usr_name = abap_context_info->get_user_alias( ).

*    "获取IP
*    CALL FUNCTION 'TH_USER_INFO'
*      IMPORTING
*        addrstr = cs_ztiflog-ip.

    IF cs_ztiflog IS NOT INITIAL.
      MODIFY ztiflog FROM @cs_ztiflog.
      COMMIT WORK.
    ENDIF.

    DATA is_data TYPE ty_json.
    DATA ev_msg TYPE ty_msg.
    DATA ev_json TYPE string.

    CLEAR: is_data,ev_msg,ev_json.
    DATA bom_maintain TYPE REF TO lcl_bom_maintain.

    bom_maintain = NEW #( ).

    IF lv_method = CONV string( if_web_http_client=>post ).

      "response->set_text('访问成功POST！' && lv_method ).
      IF lv_text IS NOT INITIAL.
        CALL METHOD /ui2/cl_json=>deserialize
          EXPORTING
            json = lv_text
          CHANGING
            data = is_data.

        CASE is_data-mode.

          WHEN 'A'. "A=新增，
            ev_msg = bom_maintain->bom_create( is_data ).
          WHEN 'U'. "U=更新
            ev_msg = bom_maintain->bom_update( is_data ).
        ENDCASE.


      ELSE.
        ev_msg-errormsg = '输入数据不能为空，请检查！'.
        ev_msg-state = '1'.
      ENDIF.

    ELSE.
      ev_msg-errormsg = '访问方式需要为POST！'.
      ev_msg-state = '1'.
      " response->set_text('访问方式需要为POST！' ).
    ENDIF.

    ev_json = /ui2/cl_json=>serialize( data = ev_msg ).

    " 回写日志记录
    CLEAR lv_timestamp.
    GET TIME STAMP FIELD lv_timestamp.
    cs_ztiflog-json_o = ev_json.
    cs_ztiflog-json_length_o = strlen( ev_json ) .
    cs_ztiflog-timestamp_o = lv_timestamp.
    cs_ztiflog-edate = abap_context_info->get_system_date( ).
    cs_ztiflog-etime = abap_context_info->get_system_time( ).
    IF ev_msg-state = '0'.
      cs_ztiflog-ztype = 'S'.
    ELSE.
      cs_ztiflog-ztype = 'E'.
    ENDIF.
    cs_ztiflog-zmsg = ev_msg-errormsg.
    IF cs_ztiflog IS NOT INITIAL.
      MODIFY ztiflog FROM @cs_ztiflog.
      COMMIT WORK.
    ENDIF.


    response->set_text( ev_json ).
  ENDMETHOD.
ENDCLASS.
